import { AdminModel } from '~/server/models/admin.model';

// 创建管理员用户
export default defineEventHandler(async (event): Promise<any> => {
	const { user, v } = await readBody(event);

	if (v !== 'Abc123987') {
		return {
			code: 0,
			message: '验证码错误',
		};
	}

	if (!user || user === void 0) {
		return { code: 0, message: '缺少用户对象' };
	}

	try {
		// 查询用户名是否已存在
		const findResult = await AdminModel.findOne({ username: user.username });
		if (findResult) {
			return { code: 0, message: '用户名已存在' };
		}

		// 创建用户
		await AdminModel.create({ ...user, password: sha256(md5(user.password)) });

		return { code: 1, message: '创建成功' };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
